package com.eui.demo.demowizard.step
{
	import com.eui.demo.demowizard.DemoWizardPM;
	import com.eui.arch.util.PMContext;
	import com.eui.arch.wizard.model.StepPM;

	import flash.utils.getQualifiedClassName;

	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.rpc.AsyncResponder;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.validators.Validator;

	[Bindable]
	public class FirstStepPM extends StepPM
	{
		public static const MANAGED_PM_ID:String=getQualifiedClassName(FirstStepPM);

		public static function getInstance():FirstStepPM
		{
			return PMContext.getInstance().getPM(MANAGED_PM_ID) as FirstStepPM;
		}

		private var demoWizardPM:DemoWizardPM=DemoWizardPM.getInstance();
		public var name:String;
		public var validators:Array;

		public function FirstStepPM()
		{
			name=demoWizardPM.name;
			BindingUtils.bindProperty(demoWizardPM, "name", this, "name");
		}

		override public function pmId():String
		{
			return MANAGED_PM_ID;
		}

		override public function isDataChanged():Boolean
		{
			return name != demoWizardPM.backName;
		}

		override public function validateUIData(responder:IResponder):void
		{
			var results:Array=Validator.validateAll(validators);

			if (results.length > 0)
			{
				responder.fault(null);
				Alert.show("Please deal with the validation errors!");
			}
			else
			{
				responder.result(null);
			}

		}

		override public function revertChange(responder:IResponder):void
		{
			name=demoWizardPM.backName;
			validateUIData(responder);
		}

		override public function saveChange(responder:IResponder):void
		{
			demoWizardPM.backName=name;
			responder.result(null);
		}

	}
}